using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MessageBoardControl
{
	/// <summary>
	/// Summary description for Tree.
	/// </summary>
	[DefaultProperty("Text"), 
		ToolboxData("<{0}:Tree runat=server></{0}:Tree>")]
	public class Tree : System.Web.UI.WebControls.WebControl
	{
		#region Properties
		private string text;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string Text 
		{
			get
			{
				return text;
			}

			set
			{
				text = value;
			}
		}

		private string imagesFolder;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string ImagesFolder 
		{
			get
			{
				return imagesFolder;
			}

			set
			{
				imagesFolder = value;
			}
		}

		private MessageBoard.Group group;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Group Group 
		{
			get
			{
				return group;
			}

			set
			{
				group = value;
			}
		}

		private MessageBoard.Category category;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Category Category 
		{
			get
			{
				return category;
			}

			set
			{
				category = value;
			}
		}

		private MessageBoard.Forum forum;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Forum Forum 
		{
			get
			{
				return forum;
			}

			set
			{
				forum = value;
			}
		}

		private MessageBoard.Thread thread;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Thread Thread 
		{
			get
			{
				return thread;
			}

			set
			{
				thread = value;
			}
		}

		
		private string urlMessageBoard;
		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URLMessageBoard 
		{
			get
			{
				return urlMessageBoard;
			}

			set
			{
				urlMessageBoard = value;
			}
		}

		private string urlGroup;
		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URLGroup 
		{
			get
			{
				return urlGroup;
			}

			set
			{
				urlGroup = value;
			}
		}

		private string urlCategory;
		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URLCategory 
		{
			get
			{
				return urlCategory;
			}

			set
			{
				urlCategory = value;
			}
		}

		private string urlForum;
		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URLForum 
		{
			get
			{
				return urlForum;
			}

			set
			{
				urlForum = value;
			}
		}

		private MessageBoard.Language language;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Language Language 
		{
			get
			{
				return language;
			}

			set
			{
				language = value;
			}
		}

		#endregion

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			output.Write("<table cellpadding=0 cellspacing=0 border=0>");
			output.Write("<tr><td valign=top><img src="+imagesFolder+"/Open.gif border=0> <a href='" + URLMessageBoard + "' class='Forum_TreeLink'>"+Translate("Discussion Boards")+"</a></td></tr>");
			if(group!=null)
			{
				output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline.gif border=0><img src="+imagesFolder+"/Open.gif border=0>  <span class='Forum_TreeLink'>"+group.Title+"</span></td></tr>");
			}

            if (category != null)
            {
                output.Write("<tr><td valign=top><img src=" + imagesFolder + "/tline.gif border=0><img src=" + imagesFolder + "/Open.gif border=0>  <a href='" + URLGroup + "' class='Forum_TreeLink'>" + category.Group.Title + "</a></td></tr>");
                output.Write("<tr><td valign=top><img src=" + imagesFolder + "/tline3.gif border=0><img src=" + imagesFolder + "/Open.gif border=0> <span class='Forum_TreeLink'>" + category.Title + "</span></td></tr>");
            }

			if(forum!=null)
			{
				output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <a href='" + URLGroup + "' class='Forum_TreeLink'>"+forum.Category.Group.Title+"</a></td></tr>");
				if(forum.Category.Group.WithCategories)
				{
					output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline3.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <a href='" + URLGroup + "' class='Forum_TreeLink'>"+forum.Category.Title+"</a></td></tr>");
					output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline4.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <span class='Forum_TreeLink'>"+forum.Title+"</span></td></tr>");
				}
				else
				{
					output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline3.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <span class='Forum_TreeLink'>"+forum.Title+"</span></td></tr>");
				}
			}

			if(thread!=null)
			{
				output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <a href='" + URLGroup + "' class='Forum_TreeLink'>"+thread.Forum.Category.Group.Title+"</a></td></tr>");
				if(thread.Forum.Category.Group.WithCategories)
				{
					output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline3.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <a href='" + URLGroup + "' class='Forum_TreeLink'>"+thread.Forum.Category.Title+"</a></td></tr>");
					output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline4.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <a href='" + URLForum + "' class='Forum_TreeLink'>"+thread.Forum.Title+"</a></td></tr>");
					output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline5.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <span class='Forum_TreeLink'>"+thread.Subject+"</span></td></tr>");
				}
				else
				{
					output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline3.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <a href='" + URLForum + "' class='Forum_TreeLink'>"+thread.Forum.Title+"</a></td></tr>");
					output.Write("<tr><td valign=top><img src="+imagesFolder+"/tline4.gif border=0><img src="+imagesFolder+"/Open.gif border=0> <span class='Forum_TreeLink'>"+thread.Subject+"</span></td></tr>");
				}
				
			}
			output.Write("</table>");
			
		}
		
		
		private MessageBoard.CustomControl customControl = MessageBoard.CustomControl.Get("Tree");

		private string Translate(string word)
		{
			return MessageBoard.Translation.Get(customControl, word, Language).Equal;
		}
	}
}
